clear;clc;clf; close all;

%% Building block-LM
%%
mws = CST_Initilization;
tic;
CST_Modeling(mws);
disp(["建模成功，所用时间为：", num2str(toc), 'S']);

%% 建模函数
function [] = CST_Modeling(mws)

    Frq = [0.5, 2]; % 工作频率,太赫兹波波长范围是3mm-30um
    FC_Property_Setup_Simplfied(mws, Frq, "time");
    % 仿真参数设置
    Px = 400; %结构X向边长
    Py = 400; %结构Y向边长
    Sy = 130; %被减去的Y向边长
    Sz = 50; %被减去的Z向边长
    Sx = 50; %x方向偏移量
    MetalThick = 0.2; %基地层金属厚度
    DielectThick = 180;
    DielectThick_up = 80;
    DH = 0; DW = 0;

    W = 40;
    L = 310;
    L_up1 = 200;
    L_up2 = 250;
    H = 140; % 立柱高度
    BaseGround = 20; % 树脂基层厚度

    % 在CST中加入结构参数，方便后续查看
    invoke(mws, 'StoreParameter', 'Px', Px);
    invoke(mws, 'StoreParameter', 'Py', Py);
    invoke(mws, 'StoreParameter', 'Sy', Sy);
    invoke(mws, 'StoreParameter', 'Sz', Sz);
    invoke(mws, 'StoreParameter', 'Sx', Sx);
    invoke(mws, 'StoreParameter', 'MetalThick', MetalThick);
    invoke(mws, 'StoreParameter', 'DielectThick', DielectThick);
    invoke(mws, 'StoreParameter', 'DielectThick_up', DielectThick_up);
    invoke(mws, 'StoreParameter', 'DH ', DH);
    invoke(mws, 'StoreParameter', 'DW ', DW);
    CST_Post = CST_MicrowaveStudio();
    CST_Post.addParameter('W', W);
    CST_Post.addParameter('BaseGround', BaseGround);
    CST_Post.addParameter('L', L);
    CST_Post.addParameter('L_up1', L_up1);
    CST_Post.addParameter('L_up2', L_up2);
    CST_Post.addParameter('H', H);

    % 组件
    FC_New_Component(mws, "介质");
    FC_New_Component(mws, "金属");

    % step 1. 建立底层基底

    FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-Py / 2', 'Py / 2', '-MetalThick', '0', 'Cu_YW', '金属', '基底');
    % CST_Post.addBrick({"-Px / 2", "Px / 2"}, {"-Py / 2", "Py / 2"}, {"-MetalThick", "0"}, "基底", "金属", "Cu_YW");

    % step 2. 建立树脂 down

    FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-Py / 2', 'Py / 2', '0', 'DielectThick', 'HTL_YW', '介质', '树脂块');
    FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-Sy / 2', 'Sy / 2', 'DielectThick-Sz', 'DielectThick', 'HTL_YW', '介质', '被减树脂块');
    CST_Post.subtractObject("介质:树脂块", "介质:被减树脂块");

    for roundtime = 1:2
        % step 3. 建立金属条
        % step 3.1 X方向流道
        FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-W/2', 'W/2', 'BaseGround', 'W+BaseGround', 'Cu_YW', '金属', '金属条_X方向流道');
        % step 3.2 Y方向金属条
        FC_Model_Brick(mws, '-W / 2', 'W / 2', '-L/2', 'L/2', 'BaseGround', 'W+BaseGround', 'Cu_YW', '金属', '金属条_Y底部');
        % step 3.3 上立柱
        FC_Model_Brick(mws, '-W / 2', 'W / 2', 'L/2-W-DW', 'L/2', 'BaseGround', 'BaseGround+H+DH', 'Cu_YW', '金属', '金属条_上立柱');
        % step 3.4 上立柱
        FC_Model_Brick(mws, '-W / 2', 'W / 2', '-L/2', '-L/2+W', 'BaseGround', 'BaseGround+H', 'Cu_YW', '金属', '金属条_下立柱');

        if roundtime == 1

            % Step 4. bool运算减去金属条
            CST_Post.subtractObject("介质:树脂块", "金属:金属条_X方向流道");
            CST_Post.subtractObject("介质:树脂块", "金属:金属条_Y底部");
            CST_Post.subtractObject("介质:树脂块", "金属:金属条_上立柱");
            CST_Post.subtractObject("介质:树脂块", "金属:金属条_下立柱");

        end

    end

    % step 5. 建立上部分 up version 2

    FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-Py / 2', 'Py / 2', 'DielectThick', 'DielectThick+DielectThick_up', 'HTL_YW', '介质', '树脂块up');
    FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-Sy / 2', 'Sy / 2', 'DielectThick-Sz', 'DielectThick', 'HTL_YW', '介质', '突出树脂块');

    for roundtime = 1:2
        % step 6. 建立金属条
        % step 6.1 X方向流道
        FC_Model_Brick(mws, '-Px / 2', 'Px / 2', '-W/2', 'W/2', 'DielectThick+DielectThick_up-BaseGround-W', 'DielectThick+DielectThick_up-BaseGround', 'Cu_YW', '金属', '金属条_X方向流道up');
        % step 6.2 Y方向金属条
        FC_Model_Brick(mws, '-W / 2-Sx', 'W / 2-Sx', '-L_up1/2', 'L_up1/2', 'DielectThick+DielectThick_up-BaseGround-W', 'DielectThick+DielectThick_up-BaseGround', 'Cu_YW', '金属', '金属条_Y底部up_Left');
        FC_Model_Brick(mws, '-W / 2+Sx', 'W / 2+Sx', '-L_up2/2', 'L_up2/2', 'DielectThick+DielectThick_up-BaseGround-W', 'DielectThick+DielectThick_up-BaseGround', 'Cu_YW', '金属', '金属条_Y底部up_Right');

        if roundtime == 1

            % Step 7. bool运算减去金属条
            CST_Post.subtractObject("介质:树脂块", "金属:金属条_X方向流道up");
            CST_Post.subtractObject("介质:树脂块", "金属:金属条_Y底部up_Left");
            CST_Post.subtractObject("介质:树脂块", "金属:金属条_Y底部up_Right");

        end

    end

end

%% 调用CSTStudio并初始化
function mws = CST_Initilization()
    path = pwd; % 获取当前m文件夹路径
    cst = actxserver('CSTStudio.application');
    mws = invoke(cst, 'NewMWS');
    % app = invoke(mws, 'GetApplicationName');
    % ver = invoke(mws, 'GetApplicationVersion');
    invoke(mws, 'FileNew');
    invoke(mws, 'DeleteResults'); % 删除之前的结果，防止程序停止运行

end
